package se.webbzon.oschi01.worldobject.appearance;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;

public abstract class AbstractAppearance3D implements Appearance3D {
	
	// The branch group associated with this java3d instance
	private final BranchGroup branchGroup;
	
	// The rendering order number associated with this java3d instance
	private final int renderingOrder;
	
	// The animation of this appearance
	private Animation3D animation;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new abstract WorldObjectAppearance. **/
	public AbstractAppearance3D() {
		this(0);
	}
	
	/** Creates a new abstract WorldObjectAppearance with a given
	 * rendering order. The rendering order is only be applied when
	 * the WorldAreaView supports AUTOMATIC_ORDER. **/
	public AbstractAppearance3D(int renderingOrder) {
		this.renderingOrder = renderingOrder;
		TransformGroup transformGroup = new TransformGroup();
		transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		branchGroup = new BranchGroup();
		branchGroup.setCapability(BranchGroup.ALLOW_DETACH);
		branchGroup.addChild(transformGroup);
		animation = null;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void onStepEnd() {
		if (animation != null)
			animation.onStepEnd(this);
	}
	
	@Override public void setAnimation(Animation3D animation) {
		this.animation = animation;
	}
	
	@Override public Animation3D getAnimation() {
		return animation;
	}
	
	@Override public void getTransform(Transform3D transform) {
		TransformGroup tg = (TransformGroup) branchGroup.getChild(0);
		tg.getTransform(transform);
	}
	
	@Override public void setTransform(Transform3D transform) {
		TransformGroup tg = (TransformGroup) branchGroup.getChild(0);
		tg.setTransform(transform);
	}
	
	@Override public int getRenderingOrder() {
		return renderingOrder;
	}
	
	@Override public abstract boolean getVisible();
	
	@Override public abstract void setVisible(boolean visible);
	
	@Override public BranchGroup getJ3dBranch() {
		return branchGroup;
	}
	
	/*============================================================
	Protected Methods
	============================================================*/
	
	/** Returns the transform group associated with this java3d instance. **/
	protected TransformGroup getTransformGroup() {
		return (TransformGroup) branchGroup.getChild(0);
	}

}
